package data;

import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;

public class UserRatingList implements Iterable<UserRating> {
	private List<UserRating> ratings;
	int size;
	private long sum;

	public UserRatingList() {
		this.ratings = new LinkedList<UserRating>();
		this.size = this.ratings.size();
		this.sum = 0;
	}

	public UserRatingList(LinkedList<UserRating> ratings) {
		this.ratings = ratings;
		this.size = this.ratings.size();
		this.sum = 0;
		for (UserRating ur : ratings) {
			this.sum += ur.Rating;
		}
	}

	public long sum() {
		return this.sum;
	}

	public double getMeanRating() {
		return sum / (ratings.size() + 1);
	}

	public double getMeanDiversity() {
		int sum = 0;
		int i = 0;
		double mean_value = this.getMeanRating();
		for (UserRating ur : ratings) {
			sum += Math.abs(ur.Rating - mean_value);
			i++;
		}
		return sum / size;
	}

	public int getRating(int UserId) {
		if (ratings == null || ratings.size() == 0)
			return (Integer) null;
		else {
			int i = 0;
			for (UserRating ur : ratings) {
				if (ur.UserId == UserId)
					return ur.Rating;
				i++;
			}
			return (Integer) null;
		}
	}

	public void add(int UserId, int Rating) {
		UserRating ur = new UserRating();
		ur.UserId = UserId;
		ur.Rating = Rating;
		this.add(ur);
	}

	public void add(UserRating ur) {
		ratings.add(ur);
		size++;
		sum += ur.Rating;
	}

	public List<Integer> intersect(UserRatingList ratingsu2) {
		List<Integer> intersection = new LinkedList<Integer>();

		for (UserRating r1 : ratingsu2) {
			for (UserRating r2 : ratings) {
				if (r1.UserId == r2.UserId) {
					intersection.add(r1.UserId);
				}
			}
		}
		return intersection;
	}

	@Override
	public Iterator<UserRating> iterator() {
		return ratings.iterator();
	}
}
